home *** CD-ROM | disk | FTP | other *** search
- #include <windows.h>
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- //
- //
- // PROCBOX.H Defines and function declarations for ProcessBox
- //
- // VERSION 2.00
- //
- // ** OVERVIEW **
- //
- // PROCBOX.DLL provides a simple way to implement time-consuming processing steps (I call these
- // "Processes" in the rest of this file) which the end-user may wish to cancel. As a programmer,
- // you provide an "_exported" callback function (the Process Box Callback, PBCALLBACK) which
- // performs actions in response to messages sent to it by PROCBOX.DLL.
- //
- // PROCBOX provides a dialog box which displays a graphical %done gauge indicator and a
- // CANCEL button.
- //
- // ** ABOUT PBCALLBACK **
- // PBCALLBACK is an _exported callback which you write. PBCALLBACK can be implemented
- // with a switch statement - similarly to other standard Windows callbacks. Like Windows
- // Hook callbacks, the switch variable is called iCode. There are iCodes (detailed below)
- // which signal initialization, deinitialization, processing and cancellation.
- //
- // One of the important functions of PBCALLBACK is to perform a small chunk of the Process
- // (eg., reading a big file from disk) when it is sent a PBC_PROCESS code. Between
- // processing of the PBC_PROCESS code, multiprocessing can occur. The PBC_PROCESS message
- // will be sent repeatedly until
- // (1) PBCALLBACK returns PBCR_END or PBCR_ERROR
- // (2) User presses "CANCEL" (in standard ProcessBox), OR
- // (3) Custom Process Dialog Box Procedure calls SetCancelState(hwndPB, TRUE)
- //
- // You should never call PBCALLBACK yourself. This is handled behind the scenes in
- // the ProcessBox(Ex), RunProcess or IterateProcess APIs. (In fact ProcessBox(Ex)
- // calls the lower-level function, RunProcess. RunProcess in turn calls the
- // lowest-level API, IterateProcess.)
- //
- // REPEAT: NEVER CALL PBCALLBACK YOURSELF!
- //
- // ** APPLICATION PROGRAMMING INTERFACE **
- //
- // Several API functions provide a flexible interface
- // for accessing the Process Dialog Box. There are three
- // methods for implementing Process Box. They are listed
- // below in increasing complexity.
- //
- // The first two implement modal dialog boxes. The calling
- // application will halt until the process is complete (like
- // calling MessageBox). The first two methods disable
- // the owner window (and child windows) and setup their own
- // PeekMessage loop (inside PROCBOX.DLL) and send PBC_PROCESS
- // messages between calls to PeekMessage.
- //
- // The last method access Process Box as a modeless dialog
- // (ie., PROCBOX.DLL does not implement the message loop)
- // You are responsible for calling IterateProcess, the function
- // which allows the dialog to process a chunk of the Process.
- // Using the last method you could, for example,
- // cause processing to occur in response to WM_TIMER messages
- // or within your own custom PeekMessage loop.
- //
- // (1) Single Callback Modal - One simple function call.
- // - Only one Callback/Process Box
- // - Process Box is destroyed after
- // Process is finished.
- //
- // (2) Multiple Callback Modal- Requires several function calls.
- // - Several Callbacks can be used (sequentially) with
- // one Process Box.
- // You handle Process Box creation/destruction.
- //
- // (3) Low-level - can be used to implement several multiprocessed Process Boxes
- // in a single application environment
- // - you must provide the IsDialogMessage() handlers for each process
- // box in your message loop and call IterateProcess to
- // process each chunk of the PBCALLBACK Process.
- //
- // Single Callback Modal Interface:
- // --------------------------------
- // ProcessBox
- // ProcessBoxEx
- //
- // The ProcessBox and ProcessBoxEx functions are similar to the MessageBox Windows API.
- // Program execution halts at ProcessBox(Ex) and does not return until the process
- // has completed.
- //
- // Multiple Callback Modal Interface:
- // ----------------------------------
- // CreateProcessBox *
- // AttachProcess *
- // RunProcess
- // DestroyProcessBox *
- //
- // (* also used in Low-Level Interface)
- //
- // The functions used in this method expose a lower level of the interface for
- // Process Box. In fact, the Single Callback Modal method is a wrapper around these functions.
- // By directly using these functions (in the order listed). You can create several
- // Process Boxes, attach different callbacks to them, and start them running.
- // Alternatively, you can create a single process box and attach and run
- // several different callbacks (sequentially) using a series of AttachProcess/RunProcess
- // calls. After you're done, call DestroyProcessBox.
- // Note: calling RunProcess suspends execution of the calling app in the same
- // way that ProcessBox(Ex) does.
- //
- // Low-Level (Multiple Callback Modeless) Interface:
- // -------------------------------------------------
- // Multiple Modal Interface Functions (-RunProcess)
- // IterateTaskProcesses
- // IterateProcess *
- // GetProcessBoxFirst *
- // GetProcessBoxNext *
- // IsProcessMessage
- //
- // * IterateTaskProcesses calls IterateProcess and GetProcessBoxFirst/Next for you
- // you shouldn't have to use these functions if you call IterateTaskProcesses
- //
- // The low-level interface uses all of the functions listed above in Multiple Callback Modal
- // Interface except for RunProcess. Instead, you should use the IterateProcess
- // function to call send a PBC_PROCESS code to PBCALLBACK and respond appropriately
- // to the callback responses. (ie., drawing the Gauge indicator, changing the Title
- // caption, etc.). In fact, RunProcess is nothing more than a PeekMessage loop
- // which contains a call to IterateProcess.
- //
- // The return value from IterateProcess indicates whether processing is NOT done
- // (PI_CONTINUE), processing IS done (PI_END), an error occurred (PI_PROCESSERROR),
- // or cancel was pressed (PI_CANCEL). You'll have to provide code to handle this and
- // call IterateProcess again (if necessary). When the process is done or failed
- // call DestroyProcessBox.
- //
- // Note: IterateProcess sends the PBC_CANCEL messages to the CALLBACK when
- // necessary. You should not do this yourself!
- //
- //
- // Custom Process Box Interface:
- // -----------------------------
- // CustomProcessBox - analogous to DialogBox. Creates a modal custom process box
- // CustomProcessBoxParam - analogous to DialogBoxParam
- // CreateCustomProcessBox - analogous to CreateDialog
- // CreateCustomProcessBoxParam - analogous to CreateDialogParam
- // SetCancelState - used inside a Custom Process Box procedure to indicate
- // that the process should be canceled
- //
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- /////////////////////////////////////////////////////////////////////////////////////////////////
- //
- //
- // Process box function declarations:
- //
- //
- //
- ////////////////////////////////////////////////////////////////////////////////////////////////
-
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // SINGLE CALLBACK MODAL INTERFACE FUNCTIONS:
- //
- //
- //
-
- //
- // ProcessBoxEx - Single-step, easy to use API.
- // Handles all the functions to setup/destroy the process dialog box
- // and start/finish a process.
- // Returns when the process is complete, cancelled or an error occurs.
- //
- // RETURNS:
- // PBE_OK (on successful completion) or
- // PBE_<ERRORCODE> (see below)
- //
-
- #ifndef _INC_PROCESSBOX
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- extern int CALLBACK ProcessBoxEx( HWND, // hwnd - owner window
- LPSTR, // lpszTitle - Title bar text
- LPSTR, // lpszMessage - only used if PB_MESSAGE is set in wFlags
- UINT, // Xpos - screen coordinate. Only used if PB_CENTER is NOT set in wFlags.
- UINT, // Ypos - screen coordinate. Only used if PB_CENTER is NOT set in wFlags.
- FARPROC, // Procedure-Instance address of user-defined CALLBACK (returned by MakeProcInstance)
- LPARAM, // lUserData - user-defined data.
- WORD) ; // wFlags - control behaviour of ProcessBox
- //
- // ProcessBox -
- // Simplified wrapper around ProcessBoxEx.
- // "default" behaviour: wFlags = PB_CENTER, lpUserData=NULL
- //
- extern int CALLBACK ProcessBox( HWND, // hwnd - owner window
- LPSTR, // lpszTitle - Title bar text
- FARPROC); // Procedure-Instance address of user-defined CALLBACK (returned by MakeProcInstance)
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // MULTIPLE CALLBACK MODAL INTERFACE FUNCTIONS:
- //
- //
- //
- //
-
-
- //
- // CreateProcessBox - Creates and initializes the process dialog box
- // RETURNS: window handle of dialog box or NULL on error
- //
- extern HWND CALLBACK CreateProcessBox(
- HWND, // hwnd - owner window
- LPSTR, // lpszTitle - Title bar text
- LPSTR, // lpszMessage - only used if PB_MESSAGE is set in wFlags
- UINT, // Xpos - screen coordinate. Only used if PB_CENTER is NOT set in wFlags.
- UINT, // Ypos - screen coordinate. Only used if PB_CENTER is NOT set in wFlags.
- WORD) ; // wFlags - control behaviour of ProcessBox
- //
- // AttachProcess - Attaches a user-defined callback procedure to a dialog box
- // - If a process is already attached to this dialog box, it is
- // sent a PBC_CLOSE code before the new process is attached.
- //
- // Notes: Use AttachProcess(hwndPB, NULL); to close the existing process
- //
- // RETURNS: PBE_INVALIDHANDLE (process box handle was invalid)
- // PBE_CLOSE (previous callback had error closing)
- // PBE_OPEN (new callback couldn't open)
- // PBE_OK
- //
- extern int CALLBACK AttachProcess(
- HWND, // hwndPB - handle of process box to attach callback to
- FARPROC, // PBCallback - procedure-instance (MakeProcInstance) of _exported Callback function
- LPARAM); // lUserData - User data
-
- #define DetachProcess(x) AttachProcess(x,NULL,NULL)
-
- //
- // DestroyProcessBox - Closes the attached callback and destroys the Process Dialog Box
- // RETURNS: PBE_CLOSE
- // PBE_OK
- //
- extern int CALLBACK DestroyProcessBox(HWND /*- hwndPB*/);
-
- //
- // RunProcess - Sets up a PeekMessage loop and starts sending PBC_PROCESS
- // messages to the callback.
- //
- // PARAMETERS:
- // HWND hwndPB - a valid handle returned by CreateProcessBox
- //
- // RETURNS:
- // PBE_OK, PBE_CANCEL, PBE_PROCESS
- //
- extern int CALLBACK RunProcess(HWND); // phwndPB - the process box to run
-
- /////////////////////////////////////////////////////////////////////
- //
- // LOW-LEVEL INTERFACE FUNCTIONS:
- //
- //
- //
- //
-
- //
- // IterateProcess - Sends one PBC_PROCESS code the PBCALLBACK
- // - Responds to PBCALLBACK Response Codes (PBCR_XXXXX)
- // RETURNS:
- // PI_CONTINUE - processing is incomplete
- // PI_END - process completed OK
- // PI_PROCESSERROR - callback returned an error
- // PI_CANCEL - user canceled operation
- //
- extern int CALLBACK IterateProcess(HWND ); // hwndPB - Process Box to iterate
-
- //
- // GetProcessBoxFirst - Returns the first Process Box in the current task's list
- // (This list is maintained by PROCBOX.DLL)
- //
- // RETURNS: hwndPB - handle of first Process Box
- // NULL - failure
- //
- extern HWND CALLBACK GetProcessBoxFirst();
-
- //
- // GetProcessBoxNext - Given a valid Process Box handle (returned by
- // CreateProcessBox), returns the next Process Box in the list
- //
- // RETURNS: hwndPB - next Process Box
- // NULL - failure
- //
- extern HWND CALLBACK GetProcessBoxNext(HWND); // hwndPB - Current process box handle
-
- //
- // IsProcessMessage - Like IsDialogMessage, but for ProcessBoxes
- // Determines if the message is for the given HWND
- // and if the HWND is a ProcessBox. If it is, then
- // the message is dispatched.
- //
- // NOTE: Passing NULL as hwndPB allows you to check whether the Message
- // is for any of the available Process Boxes
- //
- // RETURNS: TRUE - message was a Process Box message and was dispatched
- // FALSE - not a Process Box message
- //
- extern BOOL CALLBACK IsProcessMessage(HWND, // hwndPB - the PRocess Box to check
- MSG far *); // lpmsg - MSG struct (returned from PeekMessage or GetMessage)
-
-
- //
- // IterateTaskProcesses
- // - Loops once through all the ProcessBoxes in the current task
- // and calls IterateProcess for each.
- // - If IterateProcess return != PI_CONTINUE, then the loop stops
- // IterateProcess returns hwndPB (process box which caused the break)
- // and pState = the return value from IterateProcess
- //
- // IterateTaskProcesses calls GetProcessBoxFirst, GetPRocessBoxNext,
- // and IterateProcess for you. You should not have to use
- // these functions if you call IterateProcess in a message loop.
- //
- // RETURNS:
- // hwndPB - handle of last Process Box to be iterated
- // or NULL if no process boxes exist in the current task
- // (NOTE: hwndPB not necessarily = last Process Box in the task's list
- // since the loop can break if one of the processes has an error,
- // is cancelled or ends).
- //
- // RETURN PARAMETER: *lpState = IterateProcess return value
- // - if hwndPB != NULL,
- // *lpState = PI_CONTINUE (IterateProcess returned PI_CONTINUE for all ProcessBoxes)
- // or PI_????? (Loop stopped at hwndPB)
- // - if hwndPB == NULL, *lpState = NULL
-
- extern HWND CALLBACK IterateTaskProcesses(int far *);
-
- /////////////////////////////////////////////////////////////////////////
- //
- // CUSTOM PROCESS BOX INTERFACE FUNCTIONS:
- //
- //
- //
-
- //
- // CustomProcessBox - Runs a custom modal Process Dialog Box
- // - very similar to a call to DialogBox
- // - first four parameters are identical to DialogBox
- //
- // RETURNS: Same as ProcessBox(Ex)
- //
- extern int CALLBACK CustomProcessBox( HINSTANCE, // instance handle of module which contains the dialog template
- LPCSTR, // address of dialog box template name
- HWND, // handle of owner window
- FARPROC, // procedure-instance handle of dialog callback function
- FARPROC, // procedure-instance handle of process callback function
- LPARAM); // lUserData - user data
-
- //
- // CustomProcessBoxParam - Runs a custom modal Process Dialog Box
- // - analogous to DialogBox
- // - first five parameters are identical to DialogBoxParam
- //
- // RETURNS: Same as ProcessBox(Ex)
- //
- extern int CALLBACK CustomProcessBoxParam(HINSTANCE, // hinst - instance handle of module which contains the dialog template
- LPCSTR, // lpszDlgTemplate - address of dialog box template name
- HWND, // hwndOwner - handle of owner window
- FARPROC, // dlgproc - procedure-instance handle of dialog callback function
- LPARAM, // lParamInit - user-data (passed as LPARAM during WM_INITDIALOG)
- FARPROC, // procedure-instance handle of process callback function
- LPARAM); // lUserData - user data
-
- //
- // CreateCustomProcessBox - Creates a custom process box using a
- // a custom dialog template and dialog box procedure
- // - analogous to CreateDialog
- //
- // RETURNS:
- // hwndPB, window handle of Custom Process Box
- // or NULL, failure
- //
- extern HWND CALLBACK CreateCustomProcessBox(
- HINSTANCE, // hinst - instance handle of module which contains the dialog template
- LPCSTR, // lpszDlgTemplate - address of dialog box template name
- HWND, // hwndOwner - handle of owner window
- FARPROC); // dlgproc - procedure-instance handle of dialog callback function
-
- //
- // CreateCustomProcessBoxParam - Creates a custom process box using a
- // a custom dialog template and dialog box procedure
- // - analogous to CreateDialogParam
- // RETURNS:
- // hwndPB, window handle of Custom Process Box
- // or NULL, failure
- //
- extern HWND CALLBACK CreateCustomProcessBoxParam(
- HINSTANCE, // hinst - instance handle of module which contains the dialog template
- LPCSTR, // lpszDlgTemplate - address of dialog box template name
- HWND, // hwndOwner - handle of owner window
- FARPROC, // dlgproc - procedure-instance handle of dialog callback function
- LPARAM); // lParamInit - user-data (passed as LPARAM during WM_INITDIALOG)
-
- extern BOOL CALLBACK SetCancelState(
- HWND, // hwndPB - handle of Custom Process Box
- BOOL); // bCancel - set to TRUE to cause cancellation of process
-
-
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- ////
- ////
- //// END OF PROCESS BOX API FUNCTION DECLARATIONS
- ////
- ////
- ////
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////
- //
- // ProcessBox Flags:
- //
- #define PB_CENTER 0x0001 // centers Process box with respect to owner
- #define PB_MESSAGE 0x0002 // enables the message display window
- #define PB_HIDE 0x0004 // prevents the box from being shown (in ProcessBox(Ex)/CreateProcessBox)
- #define PB_NOOWNERDISABLE 0x0008 // prevents default owner disabling behaviour
- // (in ProcessBox(Ex) and RunProcess)
- //
- // ProcessBox CALLBACK iCodes (PBC_XXXXXX):
- // These values are sent to the user-defined CALLBACK
- // to indicate startup, processing, cancelation and closing
- //
- #define PBC_OPEN 1 // Indicates startup.
- // CALLBACK should allocate memory and initialize
- // variables and return success/failure.
- // Failure will abort the process.
- // No more messages will be sent to the CALLBACK.
- //
- // RETURN: TRUE (success), FALSE (failure)
-
- #define PBC_CLOSE 2 // Indicates end of operation or destruction of ProcessBox.
- // CALLBACK should free any memory it allocated (in PBC_OPEN)
- // and return TRUE. Returning false will cause
- // ProcessBox(Ex) and AttachProcess to return PBE_CLOSE (error during closing)
- //
- // NOTES: PBCallback is guaranteed to receive only 1 PBC_CLOSE
- // code for each PBC_OPEN code.
- // PBC_CLOSE is generated
- // 1) After PBCallback returns PBCR_END or PBCR_ERROR
- // 2) After PBCallback returns TRUE in response to
- // PBC_CANCEL
- // 3) When AttachProcess(hwndPB, NULL, NULL) is called
- // = (DetachProcess(hwndPB))
- // 5) When the ProcessBox is destroyed.
- // (eg.,DestroyWindow, DestroyProcessBox, application exit).
- //
- // RETURN : TRUE, FALSE
-
- #define PBC_CANCEL 3 // Indicates that Cancel button was pressed
- // RETURN: TRUE = OK to Cancel (PBC_CLOSE will be sent),
- // FALSE = Continue operation (PBC_PROCESS will be sent)
-
- #define PBC_PROCESS 4 // Indicates that processing should occur.
- // CALLBACK should process a small chunk of the Process and
- // return a Process Box CALLBACK Response
- //
- // RETURN: PBCR_ERROR = Indicates an error has occurred.
- // PBC_CLOSE will be sent.
- // PBCR_END = Finished processing normally.
- // PBC_CLOSE will be sent.
- // PBCR_CONTINUE = Processing isn't finished
- // PBC_PROCESS will be sent.
-
- //
- //
- // Process Box CALLBACK Responses
- // responses to the PBC_PROCESS iCode (see above).
- //
- #define PBCR_ERROR -1
- #define PBCR_END 0
- #define PBCR_CONTINUE 1
-
-
- //
- //
- // Process Box Error Codes:
- // These values are returned by Process Box APIs
- // IterateProcess has its own return codes (see below)
- //
-
- #define PBE_OK 0 // Function worked!
- #define PBE_OPEN -1 // callback error during PBC_OPEN
- #define PBE_PROCESS -2 // callback error PBC_PROCESS handling
- #define PBE_CLOSE -3 // callback error during PBC_CLOSE handling
- #define PBE_CANCEL -4 // user pressed CANCEL
- #define PBE_CREATE -5 // failed to create Process Dialog Box
- #define PBE_INVALIDHANDLE -6 // the handle to hwndPB was invalid or callback was invalid
-
-
- //
- // IterateProcess return values:
- //
- #define PI_CONTINUE 1
- #define PI_END 0
- #define PI_PROCESSERROR -2
- #define PI_CANCEL -4
- #define PI_INVALIDHANDLE -6
-
-
- //
- // Messages for Custom Process Box procedures
- //
- #define PM_SETGAUGE WM_USER+1 // WPARAM = iPercent (>=0, <=100)
- #define PM_SETMESSAGE WM_USER+2 // LPARAM = LPSTR lpszText
-
- #define _INC_PROCESSBOX
- #ifdef __cplusplus
- } // end #ifdef __cplusplus
- #endif
- #endif // #ifndef PROCESSBOX
-
-
-
-